package com.butang.service;

import java.lang.Math;

public class DistanceCalculator {
    private static final int EARTH_RADIUS = 6371; // 地球半径，单位为千米

    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLon/2) * Math.sin(dLon/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double distance = EARTH_RADIUS * c;
        return distance;
    }

    public static void main(String[] args) {
        double lat1 =39.316705; // 第一个点的纬度
        double lon1 = 106.895702; // 第一个点的经度
        double lat2 = 39.383403; // 第二个点的纬度
        double lon2 = 107.019597; // 第二个点的经度

        double distance = calculateDistance(lat1, lon1, lat2, lon2);
        System.out.println("两点之间的距离：" + distance + "千米");
    }
}